<template>
    <el-dialog class="ba-operate-dialog" :close-on-click-modal="false" :model-value="modelValue" @close="closeForm">
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">导入记录</div>
        </template>
        <el-scrollbar class="ba-table-form-scrollbar">
            <div class="default-main ba-table-box">
                <el-alert class="ba-table-alert" v-if="baTable.table.remark" :title="baTable.table.remark" type="info" show-icon />

                <!-- 表格顶部菜单 -->
                <!-- 自定义按钮请使用插槽，甚至公共搜索也可以使用具名插槽渲染，参见文档 -->
                <TableHeader :buttons="['delete', 'quickSearch']" quick-search-placeholder="搜索 品牌/商品名称">
                    <template #quickSearchPrepend>
                    </template>
                    <span style="margin-left: 12px;" class="btn btn-warning" @click="showImportFromFun()">导入</span>
                    <span style="margin-left: 12px;" class="btn" @click="showImportLogFun()">导入记录</span>
                </TableHeader>

                <!-- 表格 -->
                <!-- 表格列有多种自定义渲染方式，比如自定义组件、具名插槽等，参见文档 -->
                <!-- 要使用 el-table 组件原有的属性，直接加在 Table 标签上即可 -->
                <Table ref="tableRef">
                    <template #size-stock>
                        <!-- 在插槽内，您可以随意发挥，通常使用 el-table-column 组件 -->
                        <el-table-column prop="id" label="尺码/库存" width="270" align="center">
                            <template #default="scope">
                                <div style="">
                                    <table border="1" style="width: 100%; text-align: center; border-color: #fff; font-weight: normal;" cellspacing="0" >
                                        <tr>
                                            <td v-for="(item, key) in scope.row.size_with_stock" :key="key" style="width: 54px;" width="100" v-html="item.size"></td>
                                        </tr>
                                        <tr>
                                            <td v-for="(item, key) in scope.row.size_with_stock" :key="key" style="" v-html="item.stock"></td>
                                        </tr>
                                    </table>
                                </div>
                            </template>
                        </el-table-column>
                    </template>
                    <template #cycle>
                        <!-- 在插槽内，您可以随意发挥，通常使用 el-table-column 组件 -->
                        <el-table-column prop="id" label="生产周期" width="180" align="center">
                            <template #default="scope">
                                <div style="">
                                    <table border="1" style="width: 100%; text-align: center; border-color: #fff;" cellspacing="0" >
                                        <tr>
                                            <td style="width: 50%;">15天</td>
                                            <td style="width: 50%;">30天</td>
                                        </tr>
                                        <tr>
                                            <td>{{ scope.row.cycle_15 }}</td>
                                            <td>{{ scope.row.cycle_30 }}</td>
                                        </tr>
                                    </table>
                                </div>
                            </template>
                        </el-table-column>
                    </template>
                </Table>

            </div>
        </el-scrollbar>
        
    </el-dialog>
</template>

<script setup lang="ts">
import { ref, provide, onMounted, reactive } from 'vue'
import baTableClass from '/@/utils/baTable'
import { defaultOptButtons } from '/@/components/table'
import { baTableApi } from '/@/api/common'
import { useI18n } from 'vue-i18n'
import Table from '/@/components/table/index.vue'
import TableHeader from '/@/components/table/header/index.vue'

const { t } = useI18n()
const tableRef = ref()

interface Props {
    modelValue: boolean
    configGroup: anyObj
}

const props = withDefaults(defineProps<Props>(), {
    modelValue: false,
    configGroup: () => {
        return {}
    },
})

const emits = defineEmits<{
    (e: 'update:modelValue', value: boolean): void
}>()



const closeForm = () => {
    emits('update:modelValue', false)
}
let optButtons = defaultOptButtons([])

// 自定义一个新的按钮
let newButton: OptButton[] = [
    {
        render: 'basicButton', // 渲染方式:tipButton=带tip的按钮,confirmButton=带确认框的按钮,moveButton=移动按钮
        name: 'download', // 按钮名称
        title: 'download', // 鼠标放置时的 title 提示
        class: 'btn margin-left-none',
        text: '收藏', // 直接在按钮内显示的文字，title 有值时可为空
        type: '', // 按钮类型，请参考 element plus 的按钮类型
        icon: '', // 按钮 icon
        disabledTip: true, // tipButton 禁用 tip
        click: (row: TableRow, field: TableColumn) => { // 自定义点击事件
            console.log('collection')
            // collectionFun(row.id)
            collectionApi(row.id).then((e) => {
                console.log(e)
                if(e.code == 1){
                    row.collection = 1;
                }
            })
        },
        display: (row: TableRow, field: TableColumn) => { // 按钮是否显示，请返回布尔值
            return row.collection == null ? true : false;
        },
        disabled: (row: TableRow, field: TableColumn) => { // 按钮是否禁用，请返回布尔值
            // return row.file ? false : true;
        },
        attr: {} // 自定义el-button属性
    },{
        render: 'basicButton', // 渲染方式:tipButton=带tip的按钮,confirmButton=带确认框的按钮,moveButton=移动按钮
        name: 'download', // 按钮名称
        title: 'download', // 鼠标放置时的 title 提示
        class: 'btn margin-left-none',
        text: '取消收藏', // 直接在按钮内显示的文字，title 有值时可为空
        type: '', // 按钮类型，请参考 element plus 的按钮类型
        icon: '', // 按钮 icon
        disabledTip: true, // tipButton 禁用 tip
        click: (row: TableRow, field: TableColumn) => { // 自定义点击事件
            console.log('cancel collection')
            cancelCollectionApi(row.id).then((e) => {
                console.log(e)
                if(e.code == 1){
                    row.collection = null;
                }
            })
        },
        display: (row: TableRow, field: TableColumn) => { // 按钮是否显示，请返回布尔值
            return row.collection == null ? false : true;
        },
        disabled: (row: TableRow, field: TableColumn) => { // 按钮是否禁用，请返回布尔值
            // return row.file ? false : true;
        },
        attr: {} // 自定义el-button属性
    },
    {
        render: 'basicButton', // 渲染方式:tipButton=带tip的按钮,confirmButton=带确认框的按钮,moveButton=移动按钮
        name: 'edit',
        title: 'edit',
        text: '编辑',
        type: 'primary',
        class: 'btn btn-primary margin-left-none',
        icon: '',
    },{
        render: 'confirmButton',
        name: 'delete',
        title: '',
        text: '删除',
        type: 'danger',
        class: 'btn btn-danger margin-left-none',
        icon: '',
        popconfirm: {
            confirmButtonText: t('Delete'),
            cancelButtonText: t('Cancel'),
            confirmButtonType: 'danger',
            title: t('Are you sure to delete the selected record?'),
        }
    }
]

// 新按钮合入到默认的按钮数组
optButtons = newButton.concat(optButtons)




/**
 * baTable 内包含了表格的所有数据且数据具备响应性，然后通过 provide 注入给了后代组件
 */
const baTable = new baTableClass(
    new baTableApi('/admin/goods.Goods/'),
    {
        pk: 'id',
        column: [
            { type: 'selection', align: 'center', operator: false },
            { label: t('品牌名'), prop: 'brand.name', align: 'center', operatorPlaceholder: t('Fuzzy query'), operator: 'LIKE', sortable: false },
            { 
                label: t('图片'), 
                render: "image", 
                prop: 'cover', 
                align: 'center', 
                operatorPlaceholder: t('Fuzzy query'), 
                operator: 'LIKE', 
                sortable: false,
                width: 120
            },
            { label: t('商品名'), prop: 'name', align: 'center', operatorPlaceholder: t('Fuzzy query'), operator: 'LIKE', sortable: false },
            { label: t('goods.goods.color'), prop: 'color', align: 'center', render: 'string', operator: false },
            { label: t('官方售价'), prop: 'price', align: 'center', operator: 'RANGE', sortable: false },
            { 
                label: t('供货率'), 
                prop: 'supply_discount', align: 'center', 
                operator: 'RANGE', sortable: false, 
                render: "string" , 
                renderFormatter: (row: TableRow, field: TableColumn, value: any, column: TableColumnCtx<TableRow>, index: number) => {
                    return value == null ? '' : value+'%';
                }
            },
            { label: t('供货价格'), prop: 'supply_price', align: 'center', operator: 'RANGE', sortable: false },
            { 
                label: t('尺码/库存'), 
                prop: 'supply_discount', 
                align: 'center', 
                operator: 'RANGE', sortable: false, 
                render: 'slot', 
                slotName: 'size-stock'
            },
            
            { 
                label: t('生产周期'), 
                prop: 'cycle_15', 
                align: 'center', 
                operatorPlaceholder: t('Fuzzy query'), 
                operator: 'LIKE', 
                sortable: false,
                render: 'slot', 
                slotName: 'cycle'
            },
            { 
                label: t('goods.goods.create_time'), 
                prop: 'create_time', 
                align: 'center', 
                render: 'datetime', 
                operator: 'RANGE', 
                sortable: false, 
                width: 100, 
                timeFormat: 'yyyy-mm-dd',
            },
            { label: t('Operate'), align: 'center', width: 100, render: 'buttons', buttons: optButtons, operator: false },
        ],
        dblClickNotEditColumn: ['all'],
        defaultOrder: { prop: 'id', order: 'desc' },
    },
    {
        defaultItems: { name: null, color: null, size: null, stock: null, stock_total: null, cover: null, price: 0, supply_discount: 0, supply_price: 0, cycle_15: null, cycle_30: null, upstream_link: null },
    }
)

provide('baTable', baTable)

onMounted(() => {
    baTable.table.ref = tableRef.value
    baTable.mount()
    baTable.getIndex()?.then(() => {
        baTable.initSort()
        baTable.dragSort()
    })
})
</script>

<style scoped lang="scss"></style>
